//  Created by 莫瑞权 on 2022/6/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TF_1vN_LiveMain: TF1vnMainViewController {

    @objc public var drawType:TalkfunDrawType =  TalkfunDrawTypeStroke
    
    @objc public var videoFrequentlyClick:Bool = false
    @objc public var audioFrequentlyClick:Bool = false
    


    public override func viewDidLoad() {
        super.viewDidLoad()
        

    }

  @objc func askQuestionsClick()
    {
        self.isShowTopViewAndBottomView(false)
        self.navigationBar.askQuestions.clearRedDot()
        self.isShowVotingArea(false)
        if (self.qskQuestionsView.frame.origin.x <= self.superView.frame.size.width) {
            self.isShowqskQuestionsView(true)
        }else{
            self.isShowqskQuestionsView(false)
        }
        if (self.qskQuestionsView.frame.origin.x <= self.superView.frame.size.width&&self.qskQuestionsView.array.count==0) {
            //获取提问列表
            self.getAListOfQuestions()
        }
    }
    @objc func qskQuestions()
    {
        self.qskQuestionsView.frame = CGRect(x: self.superView.frame.size.width, y: 0, width: 335, height: self.superView.frame.size.height)
        self.qskQuestionsView.backgroundColor = UIColor(red: 28/255.0, green: 31/255.0, blue: 35/255.0, alpha: 0.98)
        self.superView.addSubview(self.qskQuestionsView)
        self.qskQuestionsView.send.addTarget(self, action: #selector(qskQuestionsFieldButtonClick), for: .touchUpInside)
   
    }
   @objc func qskQuestionsFieldButtonClick()
    {
        self.keyboardTool.qskQuestions.isHidden = false
        self.keyboardTool.qskQuestions.becomeFirstResponder()
        self.keyboardTool.expressionButton.isHidden = true
        self.keyboardTool.commonLanguage.isHidden = true
        self.keyboardTool.flower.isHidden = true

    }
    //投票列表
   @objc func addVotingArea()
    {
        let votingAreaY:CGFloat = 0.0
        let votingAreaHeight:CGFloat = self.superView.frame.size.height
        let votingAreaW:CGFloat =  335
        let votingAreaX:CGFloat =  self.superView.frame.size.width
        self.votingArea.frame = CGRect(x: votingAreaX, y: votingAreaY, width: votingAreaW, height: votingAreaHeight)
        self.superView.addSubview(self.votingArea)
        //TODO: 获取投票详情
        self.votingArea.voteBlock = {[weak self] vid in
           guard let `self` = self else { return }
            self.getVoteDetail(vid)
        }
        //TODO: 执行投票事件
        self.votingArea.voteDetail.newVoteBlock = {[weak self] (vid,optionsArray) in
            guard let `self` = self else { return }
        
            let submit = TFSubmit()
            submit.vid = vid
           
            submit.option =  NSMutableArray.init(array: optionsArray)
            self.talkfunSDK.voteSubmit(submit) {[weak self] (code, error) in
                guard let `self` = self else { return }
                
                if (code==0) {
                    self.TF_toast("投票成功")
                    self.navigationBar.voteBtn.clearRedDot()
                    self.votingArea.successfulVote(vid as NSString)
                    self.votingArea.voteDetail.successfulVote()
                  }else
                  {
                    self.TF_toast("投票失败")
                  }
            }
          
        }

    }

    func isShowqskQuestionsView(_ show:Bool)
    {
        UIView.animate(withDuration: 0.5) {
                if (show) {
                 self.qskQuestionsView.frame = CGRect(x: self.superView.frame.size.width - 335, y: self.qskQuestionsView.frame.origin.y, width: 335, height: self.superView.frame.size.height)

            }else{
                 self.qskQuestionsView.frame = CGRect(x: self.superView.frame.size.width, y: self.qskQuestionsView.frame.origin.y, width: 335, height: self.superView.frame.size.height)

            }
        }


    }
    func getAListOfQuestions()
    {
        self.talkfunSDK.getAList {[weak self] code, list, error in
            guard let `self` = self else { return }
            self.qskQuestionsView.updatedQuestionListUI(list!)
        }

    }
   // #pragma mark 各种网络错误
   @objc func addTallkFenPromptTool()
    {

        self.promptTool.clipsToBounds  = false
        self.promptTool.whiteboardWidth = self.superView.frame.size.width
        self.promptTool.clipsToBounds = false
        self.promptTool.frame = self.superView.bounds
        self.promptTool.backgroundColor = UIColor.clear
        self.promptTool.isUserInteractionEnabled  = false
        self.promptTool.backgroundImage.isHidden = false

    }
    //#pragma mark 成为第一响应者
   @objc func inputFieldButtonClick()
    {
        self.keyboardTool.qskQuestions.isHidden = true
        self.keyboardTool.expressionButton.isHidden = false
        self.keyboardTool.commonLanguage.isHidden = false
        if (self.modetype == "6"){
               self.keyboardTool.flower.isHidden = false
           }else{
               self.keyboardTool.flower.isHidden = true
           }
        //图标改回来
        self.keyboardTool.expressionButton.isSelected = false
        self.keyboardTool.expressionButton.setImage(UIImage.init(named: "HTLive.bundle/huisebiaoqing"), for: .normal)
        self.keyboardTool.inputField.becomeFirstResponder()

    }
    @objc func addChatList()
    {
        let   ChatAreaY:CGFloat = 0.0
        let   ChatAreaHeight:CGFloat =  self.superView.frame.size.height
        let   ChatAreaW:CGFloat =  335
        let   ChatAreaX:CGFloat =  self.superView.frame.size.width
        self.chatArea.frame = CGRect(x: ChatAreaX, y: ChatAreaY, width: ChatAreaW, height: ChatAreaHeight)
        self.superView.addSubview(self.chatArea)
        
    }
  //  #pragma mark TODO:初始化SDK-----------------
    @objc func inittalkfunSDK(_ token:String,courseType:TalkfunCourseType)->TalkfunSDKLive
    {
        let params = TalkfunSDKLiveInitParams()
        params.accessToken = token
        params.courseType = courseType
        self.talkfunSDK.enterRoom(params)
        self.talkfunSDK.orientationMode = .fixedLandscape
        self.talkfunSDK.scalingMode = .fill
        return self.talkfunSDK
    }

 //   #pragma mark  -----------送花---------
   // #pragma mark  花朵总数
   @objc public func onFlowerTotal(_ total: Int) {
       self.keyboardTool.flower.drawRedDotWidthNumber(NSString.init(format: "%i", total) as String)
    }
   // #pragma mark  接收别人发送的花朵
    @objc public func onFlowerSend(_ result: TFLiveFlowerData) {

        self.chatArea.setFlowersDict(result)
        self.keyboardTool.flower.drawRedDot()
        if (result.avatar.count>0) {
            let amount:NSInteger = result.amount
            var Fireworks:String = ""
            if (amount==1) {
                Fireworks =  "[Fireworks]"
            }else  if (amount == 2) {
                Fireworks =  "[Fireworks][Fireworks]"
            }else if (amount == 3) {
                Fireworks =  "[Fireworks][Fireworks][Fireworks]"
            }
            Fireworks = "送给主播" + Fireworks
            self.loadImage(Fireworks, avatar: result.avatar as String)
       
        }
    }
    //#pragma mark 主播回答了问题
   @objc public func onQuestionReply(_ result: TFQuestionData) {
       self.qskQuestionsView.questionPeply(result)
        if(self.qskQuestionsView.frame.origin.x==self.superView.frame.size.width) {
            self.navigationBar.askQuestions.drawRedDot()
        }
    }
 //   #pragma mark 提问删除
    @objc public func onQuestionDelete(_ result: TFQuestionData) {
        self.qskQuestionsView.replyTodelete(result)
    }
  //  #pragma mark 接收别人的提问
    @objc public func onQuestionAsk(_ result: TFQuestionData) {
        self.qskQuestionsView.addQskQuestionsDict(result)
         if(self.qskQuestionsView.frame.origin.x==self.superView.frame.size.width) {
             self.navigationBar.askQuestions.drawRedDot()
          }
    }
  //  #pragma mark 提问审核通过
    @objc public func onQuestionAudit(_ result: TFQuestionData) {
        self.qskQuestionsView.addQskQuestionsDict(result)
        if(self.qskQuestionsView.frame.origin.x==self.superView.frame.size.width) {
            self.navigationBar.askQuestions.drawRedDot()
        }
    }
    
  @objc func videoH()->CGFloat
    {
        var videoH:CGFloat = 0.0
        if (UIDevice.current.userInterfaceIdiom == .phone)
        {
            videoH = 80+8
        }else{
            videoH = 139+10
        }
        return videoH
    }
    
    //#pragma mark 实时课件下载开关状态
  @objc public func onDocumentDownload(_ enable: Bool)
    {
        //只有显示的时候才请求课件数据
      self.navigationBar.updateCourseDownStatus(enable)
        
    }
    

   
}
